package shuixian.jing.discuz.activity;

import shuixian.jing.discuz.R;
import shuixian.jing.discuz.activity.base.FrameActivity;
import shuixian.jing.discuz.controls.dialog.PgProgressDialog;
import shuixian.jing.discuz.json.JSONLogin;
import shuixian.jing.discuz.utility.MyHttpClient;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityLogin extends FrameActivity {

	private TextView mTextViewCheckOk;
	private static final int PROGRESS_DIALOG = 2;
	private EditText mEditTextName ;
	private EditText mEditTextPWD ;
	private PgProgressDialog mpd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login);
		initView() ;
		
		mTextViewCheckOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
		
				 /*try {
				 
				 // 创建一个默认的HttpClient
				 ActivityLogin.this.showDialog(PROGRESS_DIALOG);
				 HttpClient httpclient = MyHttpClient.getInstance();
				 
				 // 创建一个GET请求
				 
				  HttpGet request = new HttpGet(MySiteConfig.web_rul+"api/mobile/index.php?module=login&loginsubmit=yes&username=admin&password=admin");
				  
				  // 发送GET请求，并将响应内容转换成字符串
				  BasicResponseHandler bHandler =   new BasicResponseHandler();
				  String response = httpclient.execute(request,bHandler);
				  
				  HttpGet pmHttpGet = new HttpGet(MySiteConfig.web_rul+"api/mobile/index.php?module=mypm");
				  
				  response = httpclient.execute(pmHttpGet,bHandler);
				  
				  JSONTokener jsonParser = new JSONTokener(response); 
				  
				  JSONObject person = (JSONObject) jsonParser.nextValue();  
				  person = person.getJSONObject("Variables");
				  showLog(response);
				  showToast(person.getString("member_username"));
				  
				  //跳转到主界面
				  startActivity(new Intent(ActivityLogin.this, ActivityMain.class));
				  
				  } catch (ClientProtocolException e) {
				 
				  e.printStackTrace();
				  
				 } catch (IOException e) {
				 
				  e.printStackTrace();
				  
				  } catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
				ActivityLogin.this.showDialog(PROGRESS_DIALOG);
				LoginTask task = new LoginTask();
				task.execute(new String[]{mEditTextName.getText().toString(),mEditTextPWD.getText().toString()});
				
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int dialogId) {
		Dialog dialog = null;
		switch (dialogId) {
		case PROGRESS_DIALOG:
			mpd = new PgProgressDialog(this);
			mpd.setProgressStyle(PgProgressDialog.STYLE_TRANSPARENT);
			mpd.show();
			break;
		}
		return dialog;
	}
	/**
	 * 异步登陆
	 * @author Administrator
	 *
	 */
	class LoginTask extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			String api = "api/mobile/index.php?module=login&loginsubmit=yes&username="+ params[0]+"&password="+params[1];
			
			return MyHttpClient.executeGet(api);
		}

		@Override
		protected void onPostExecute(String result) {
			JSONLogin login = JSONLogin.getInstance();
			login.parseJSON(result);
			showLog(result);
			if(login.getLoginName() !=null && login.getLoginName().length() != 0 ){
				Toast.makeText(ActivityLogin.this, login.getLoginName(), Toast.LENGTH_LONG).show();  
				 startActivity(new Intent(ActivityLogin.this, ActivityMain.class));
			}else{
				
			Toast.makeText(ActivityLogin.this, login.getErrorMsg(), Toast.LENGTH_LONG).show();  
			mpd.hide();
			}
		}
		
	}
	@Override
	protected void initView() {
		
		mEditTextName = (EditText)findViewById(R.id.qqId);
		mEditTextPWD= (EditText)findViewById(R.id.passWord);
		mTextViewCheckOk = (TextView) findViewById(R.id.checkOk);
	}
	
	

}
